package com.code.modules.base.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.code.common.utils.MD5Util;
import com.code.modules.base.mapper.UserroleMapper;
import com.code.modules.base.model.Userrole;
import com.code.modules.base.service.UserService;
import com.code.modules.base.mapper.UserMapper;
import com.code.modules.base.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author MyBatisPlus
 * @since 2021-10-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private UserroleMapper userroleMapper;


    @Override
    public User findUserByusername(String username) {
        User user = userMapper.getUserByusername(username);
        return user;
    }

    @Override
    public boolean checkUser(String username, String password) {
        User user = userMapper.getUserByusername(username);
        if(user != null) {
            String pwd_md5 = MD5Util.md5(password,user.getSalt());
            if(pwd_md5.equals(user.getPassword())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<User> getPageList(String condition) {
        return userMapper.selectPageList(condition);
    }

    @Override
    public boolean changeUserPwd(Integer uid, String pwd, String salt) {
        int row = userMapper.changePwd(uid,pwd,salt);
        if(row>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean setUserAatar(Integer uid, String avatarurl) {
        int row = userMapper.setAatar(uid,avatarurl);
        if(row>0) {
            return true;
        }
        return false;
    }


    @Override
    public void setUserRoles(Integer userid, List<Integer> roleids) {
        userroleMapper.delUserRolesById(userid);
        for (Integer id : roleids) {
            Userrole ur = new Userrole();
            ur.setUserid(userid);
            ur.setRoleid(id);
            userroleMapper.insert(ur);
        }
    }







}
